<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                template="/layout/main.xhtml">
    <ui:define name="title"> <h:outputText rendered="#{viewPostBean.post.title ne null}"
                                           value="#{viewPostBean.post.title} - "/>Posts</ui:define>
    <ui:define name="content">

        <h:form>
            <h2>Posts</h2>
            <h:link outcome="post-create" value="New Post"/>

            <f:metadata>
                <f:viewParam name="view" id="view" value="#{viewPostBean.id}"/>
                <f:viewParam name="edit" id="edit" value="#{viewPostBean.edit}"/>
            </f:metadata>

            <h:panelGroup rendered="#{viewPostBean.id ne null and viewPostBean.post eq null}">
                <h3>No post with this ID is available.</h3>
                <h:link outcome="post" value="Back to Post List" styleClass="commandlink"/>
            </h:panelGroup>

            <h:panelGroup rendered="#{viewPostBean.id eq null}">
                <h3>Listing Posts</h3>
                <ui:repeat value="#{posts}" var="currPost">
                    <div class="post">
                        <div class="title">
                            <div style="float:right; font-size: 60%;">#{currPost.date}</div>
                            <strong>#{currPost.title}</strong>
                        </div>
                        <h:panelGroup rendered="#{currPost.user ne null}">
                            <div class="author">By #{currPost.user.name} #{currPost.user.surname} -</div>
                        </h:panelGroup>
                        <h:outputText converter="#{stringLengthConverter}" value="#{currPost.content}">
                            <f:param id="length" name="paramLength" value="100"/>
                        </h:outputText>
                        <h:link outcome="post" value="Read More" style="margin-left: 0.5em;">
                            <f:param name="view" value="#{currPost.id}"/>
                        </h:link>
                    </div>
                    <br/>
                </ui:repeat>
            </h:panelGroup>

            <h:panelGroup rendered="#{viewPostBean.id ne null and viewPostBean.post ne null}">
                <div class="post">
                    <div class="title">
                        <div style="float:right; font-size: 60%;">#{viewPostBean.post.date}</div>
                        <strong>#{viewPostBean.post.title}</strong>
                        <h:panelGroup
                                rendered="#{!viewPostBean.edit and loginBean.user ne null and (loginBean.user.id eq viewPostBean.post.user.id)}">
                            <h:link outcome="post" value="Edit Post">
                                <f:param name="view" value="#{viewPostBean.id}"/>
                                <f:param name="edit" value="true"/>
                            </h:link>
                        </h:panelGroup>
                    </div>
                    <h:panelGroup rendered="#{currPost.user ne null}">
                        <div class="author">By #{viewPostBean.post.user.name} #{viewPostBean.post.user.surname} -</div>
                    </h:panelGroup>
                    <h:outputText value="#{viewPostBean.post.content}" escape="false"/>
                </div>
                <h:link outcome="post" value="Back to Post List" styleClass="commandLink"/>

                <h:panelGroup rendered="#{loginBean.user ne null and (loginBean.user.id eq viewPostBean.post.user.id)}">
                    <h:panelGroup rendered="#{viewPostBean.edit}">
                        <div class="post">
                            <h:panelGrid columns="2">
                                <h:outputLabel value="Title"/>
                                <h:inputText value="#{viewPostBean.editTitle}"/>
                                <h:outputLabel value="Content"/>
                                <h:inputTextarea value="#{viewPostBean.editContent}"/>
                                <h:outputLabel value=""/>
                                <h:commandButton actionListener="#{viewPostBean.updatePost}" value="Update"/>
                            </h:panelGrid>
                        </div>
                    </h:panelGroup>
                </h:panelGroup>
            </h:panelGroup>

        </h:form>
    </ui:define>
</ui:composition>
